<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">后台管理系统</div>
            <el-form :model="param" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="tel">
                    <el-input v-model="param.tel" placeholder="电话号码" :maxlength="11">
                        <el-button slot="prepend" icon="el-icon-user"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="密码"
                        v-model="param.password"
                        @keyup.enter.native="userLogin(param)"
                    >
                        <el-button slot="prepend" icon="el-icon-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="userLogin(param)">登录</el-button>
                </div>
                <div class="login-btn">
                    <el-button type="primary" @click="registerVisible=true">注册</el-button>
                </div>
                <p class="login-tips">Tips : 用户名和密码随便填。</p>
            </el-form>
            <el-dialog :visible.sync="registerVisible" class="register-page" title="注册"  :before-close="handleClose">
                <div>
                    <el-form ref="addForm" :model="formUser" label-width="80px" :rules="rules">
                        <el-form-item label="姓名" prop="userName">
                            <el-input v-model="formUser.userName"></el-input>
                        </el-form-item>
                        <el-form-item label="联系电话"  prop="tel">
                            <el-input v-model="formUser.tel" :maxlength="11" @blur="isPhone"></el-input>
                        </el-form-item>
                        <el-form-item label="身份证号"  prop="identityCardNumber">
                            <el-input v-model="formUser.identityCardNumber" :maxlength="23" @blur="isId"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input type="password" v-model="formUser.password"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="password2">
                            <el-input type="password" v-model="formUser.password2"></el-input>
                        </el-form-item>
                        <el-form-item>

                            <el-button type="primary" @click="Register" style="width: 48%;height:40px;" :disabled="watchpassword">确 定</el-button>
                            <el-button @click="cancelRegister" style="width:48%;;height:40px;">返回</el-button>
                        </el-form-item>
                    </el-form>
                </div>

            </el-dialog>
        </div>
    </div>
</template>

<script>
    import { requestRegister, requestUserLogin } from '../../api';

export default {
    data: function() {
        return {
            param: {
                username: '',
                password: '',
                identityCardNumber:'',
                tel:''
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                identityCardNumber:[
                    { required: true, message: '请填写证件号码', trigger: 'blur' },
                    {
                        pattern: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/,
                        message: '证件号码格式有误！',
                        trigger: 'blur'
                    }
                ],
                tel:[
                    { min: 11, max: 11, message: '输入11位电话号码', trigger: 'blur' },
                    {
                        required: true,
                        message: '请输入手机号',
                        trigger: 'blur'
                    },
                    {
                        pattern: /^1(3|4|5|6|7|8|9)\d{9}$/,
                        message: '手机号无效',
                        trigger: 'blur'
                    }
                ],
            },
            registerVisible:false,
            phone:false,
            Id:false,
            formUser: {
                userName: "",
                tel: "",
                identityCardNumber:"",
                password:"",
                password2:"",
            },
            options: [{
                value: '2',
                label: '员工'
            }, {
                value: '3',
                label: '用户'
            }],
            date:""
        };
    },
    computed:{
        watchpassword(){
            if(this.formUser.password === this.formUser.password2 && this.formUser.password !== '' && this.formUser.tel !== '' && this.formUser.userName !== '' && this.formUser.identityCardNumber !== ''){
                return false;
            }
            else {
                return true;
            }
        }
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    /*this.$store.dispatch("checkLogin",this.param);*/
                    //this.$router.push('/');
                    /*this.$message.success('登录成功');
                    localStorage.setItem('ms_username', this.param.username);
                    this.$router.push('/');*/
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        async userLogin(){
            let json = {tel:this.param.tel,password:this.param.password}
            const res = await requestUserLogin(json);
            console.log(res.isRight)
            if(res.isRight === false){
                this.$message.error("密码错误")
            }
            else {
                let user = res.data;
                let se = [[user]];
                sessionStorage.setItem("userinfo", JSON.stringify(se));
                this.$store.state.user = user;
                let wsUtil = this.$store.state.wsUtil;
                wsUtil.buildConn("localhost:8080",user.id);
                switch(this.$store.state.user.mark) {
                    case(1):
                        this.$router.push("/dashboard")
                        break;
                    case(2):
                        this.$router.push("/dashboard")
                        break;
                    case(3):
                        this.$router.push("/dashboard")
                        break;
                }
            }
        },
        async Register(){
            let json1 = {tel:this.formUser.tel,userName:this.formUser.userName,password: this.formUser.password,identityCardNumber:this.formUser.identityCardNumber,mark:3}
            const res = await requestRegister(json1).catch(() => {
                return "errorCnn";
            });
            if (res != "errorCnn") {
                if (res == "duplicate") {
                    this.$message({
                        message: "一个手机号仅限一个用户！",
                        type: "warning"
                    });
                    return;
                } else if (res == "error") {
                    this.$message({
                        message: "服务器内部错误！",
                        type: "warning"
                    });
                    return;
                }
                this.$message({
                    message: "添加用户成功！",
                    type: "success"
                });
                this.registerVisible = true;
                this.$emit("closeRegister")
            }
            this.formUser = {}
        },
        cancelRegister(){
            this.registerVisible = false;
        },
        async isPhone(){
            var value = this.formUser.tel
            var pattern = /^1[34578]\d{9}$/
            if(value!=""&&pattern.test(value)){
                this.phone = true;
            }
            else {this.phone = false}
        },
        async isId(){
            var value = this.formUser.identityCardNumber
            var pattern = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/
            if(value!=""&&pattern.test(value)){
                this.Id = true;
            }
            else {this.Id = false}
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => {
                });
        }
    },
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/login2.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
    .prl{
        padding-right: 50px;
        padding-right: 50px;
    }
</style>
